﻿using System.ComponentModel;
using System.Windows.Forms;
using CDIO.POS;
using CDIO.Util;
using CDIO.DB;
using System.Collections.Generic;
namespace 餐厅模块
{
    public partial class Restaurant : Form
    {
        private List<Pos> Poses;
        private const string SNKey = "ServerName";
        private const string DNKey = "DatabaseName";
        private const string UNKey = "UserName";
        private const string PSWKey = "Password";
        private string ServerName;
        private string DatabaseName;
        private string UserName;
        private string Password;
        public Restaurant()
        {
            InitializeComponent();
            Poses = new List<Pos>();
            ServerName = "127.0.0.1";
            DatabaseName = "schoolcard";
            UserName = "card";
            Password = "card";
            if (ReadRegConfig() < 4)
            {
                Configure();
            }
        }

        private void RecvBlackListThread()
        {
            while (true)
            {
                string blackNo;
                bool suspend;
                Net netUtil = new Net();
                RestaurantLog.Items.Add(System.DateTime.Now + "接收挂失信息……");
                if (netUtil.RecvBlackList(out blackNo, out suspend))
                {
                    RestaurantLog.Items.Add(System.DateTime.Now+"收到" + (suspend ? "挂失" : "解挂") + "卡号:" + blackNo);
                    RestaurantLog.Items.Add(System.DateTime.Now + "通知消费机……");
                    foreach (Pos p in Poses)
                    {
                        p.mutex.WaitOne();
                        bool result = p.SuspendCard(blackNo, suspend);
                        RestaurantLog.Items.Add(System.DateTime.Now + "通知" + p.POS_id + "号机" + (result ? "成功" : "失败"));
                        p.mutex.ReleaseMutex();
                    }
                }
                else
                {
                    RestaurantLog.Items.Add(System.DateTime.Now + "网络错误，稍后重试……");
                    System.Threading.Thread.Sleep(5000);
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (MessageBox.Show("确认退出?", "请确认", MessageBoxButtons.YesNo) == DialogResult.No)
                e.Cancel = true;
            SaveRegConfig();
            base.OnClosing(e);
        }

        //添加消费机
        private void button1_Click(object sender, System.EventArgs e)
        {
            int comport = decimal.ToInt32(ComPort.Value);
            int posit =  decimal.ToInt32(PosId.Value);
            Pos pos = new Pos(comport,posit);
            bool posconnect = pos.IsConnected();
            Poses.Add(pos);
            ListViewItem newpos = new ListViewItem(
                new string[]{
                    "COM"+comport,
                    ""+posit,
                    (posconnect?"已连接":"未连接")},
                    null,
                    posconnect?System.Drawing.Color.Black:System.Drawing.Color.Gray,
                    System.Drawing.Color.White,
                    this.checkBox1.Font
                );
            PosList.Items.Add(newpos);
            ComPort.Value++;
            PosId.Value++;
        }

        private void OnTimerRead()
        {
            DAO dao = new CDIO.DB.DAO(false);
            dao.DatabaseServer = ServerName;
            dao.DatabaseName = DatabaseName;
            dao.UserName = UserName;
            dao.Password = Password;
            foreach (Pos p in Poses)
            {
                List<Record> list = p.ReadAllRecord(this.RestaurantLog);
                //RestaurantLog.AutoScrollOffset = new System.Drawing.Point(0, RestaurantLog.ClientRectangle.Bottom);
                if (list != null && list.Count > 0)
                {
                    foreach (Record r in list)
                    {
                        int rows = (int)dao.Excute("insert into tradelog values(" +
                             r.Card +
                             ",'" + r.date + "'" +
                             "," + r.Consume + "," + r.Balance
                             , true);

                    }
                }
            }

        }

        private void ReadRecordTimer_Tick(object sender, System.EventArgs e)
        {
            OnTimerRead();
        }

        //删除消费机
        private void button2_Click(object sender, System.EventArgs e)
        {
            if (PosList.SelectedItems.Count>0)
            {
                foreach (ListViewItem li in PosList.SelectedItems)
                {
                    PosList.Items.Remove(li);
                }
            }
            else
            {
                MessageBox.Show("请选中一个消费机");
            }
        }

        private void Configure()
        {
            ConfigDialog c = new ConfigDialog();
            c.servername.Text = ServerName;
            c.databasename.Text = DatabaseName;
            c.username.Text = UserName;
            c.password.Text = Password;
            if (DialogResult.OK == c.ShowDialog(this))
            {
                ServerName = c.servername.Text;
                DatabaseName = c.databasename.Text;
                UserName = c.username.Text;
                Password = c.password.Text;
            }
        }

        private int ReadRegConfig()
        {
            int readed = 0;
            string regval = RegisterOprate.ReadRegData(SNKey) as string;
            if (regval != null)
            {
                ServerName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(DNKey) as string;
            if (regval != null)
            {
                DatabaseName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(UNKey) as string;
            if (regval != null)
            {
                UserName = regval;
                readed++;
            }
            regval = RegisterOprate.ReadRegData(PSWKey) as string;
            if (regval != null)
            {
                Password = regval;
                readed++;
            }
            return readed;
        }

        private void SaveRegConfig()
        {
            RegisterOprate.SaveRegData(SNKey, ServerName);
            RegisterOprate.SaveRegData(DNKey, DatabaseName);
            RegisterOprate.SaveRegData(UNKey, UserName);
            RegisterOprate.SaveRegData(PSWKey, Password);
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Configure();
        }

        private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
        {
            ReadRecordTimer.Enabled = checkBox1.Checked;
            if (checkBox1.Checked)
            {
                OnTimerRead();
            }
            ReadRecordTimer.Interval = decimal.ToInt32(Period.Value) * 3600000;
        }

    }
}
